package com.hontech.bvapp2.ui.parts.cargo

import android.content.Context
import android.util.AttributeSet
import android.view.LayoutInflater
import android.widget.FrameLayout
import android.widget.TextView
import com.hontech.bvapp2.R
import com.hontech.bvapp2.device.cp.Cargo
import com.hontech.bvapp2.ui.view.LabelState
import com.hontech.bvapp2.utils.registerEvent
import com.hontech.bvapp2.utils.unregister
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode

class CargoStatusParts (context: Context, attrs: AttributeSet) : FrameLayout(context, attrs) {

    private val cargoId: Int
    private val cyInner: LabelState
    private val cyExtern: LabelState
    private val cyTop: LabelState
    private val cyBottom: LabelState
    private val pickOrigin: LabelState
    private val compChecked: LabelState
    private val cargoBtn: LabelState
    private val cargoOrigin: LabelState
    private val pickAlm: LabelState
    private val cargoAlm: LabelState

    init {
        val view = LayoutInflater.from(context).inflate(R.layout.parts_cargo_status, this)
        val attr = context.obtainStyledAttributes(attrs, R.styleable.CargoStatusParts)
        val cargo = attr.getInteger(R.styleable.CargoStatusParts_status_cargo, 0)
        attr.recycle()

        cargoId = cargo
        val title = view.findViewById<TextView>(R.id.id_cargo_status_title)

        if (cargo == 1) {
            title.text = "货架1状态"
        } else if (cargo == 2) {
            title.text = "货架2状态"
        }

        cyInner = view.findViewById(R.id.id_cargo_status_cy_inner)
        cyExtern = view.findViewById(R.id.id_cargo_status_cy_extern)
        cyTop = view.findViewById(R.id.id_cargo_status_cy_top)
        cyBottom = view.findViewById(R.id.id_cargo_status_cy_bottom)
        pickOrigin = view.findViewById(R.id.id_cargo_status_pick_origin)
        compChecked = view.findViewById(R.id.id_cargo_status_cy_comp_checked)
        cargoBtn = view.findViewById(R.id.id_cargo_status_cargo_btn)
        cargoOrigin = view.findViewById(R.id.id_cargo_status_cargo_origin)
        pickAlm = view.findViewById(R.id.id_cargo_status_pick_alm)
        cargoAlm = view.findViewById(R.id.id_cargo_status_cargo_alm)
    }

    override fun onWindowVisibilityChanged(visibility: Int) {
        super.onWindowVisibilityChanged(visibility)
        if (visibility == VISIBLE) {
            registerEvent(this)
        } else {
            unregister(this)
        }
    }


    @Subscribe(threadMode = ThreadMode.MAIN_ORDERED)
    fun onStatus(status: Cargo.Status) {
        if (status.cargoId != cargoId) {
            return
        }
        cyInner.set(status.isCyInner())
        cyExtern.set(status.isCyExtern())
        cyTop.set(status.isCyTop())
        cyBottom.set(status.isCyBottom())
        pickOrigin.set(status.isPickOrigin())
        compChecked.set(status.isCompChecked())
        cargoBtn.set(status.isCargoBtn())
        cargoOrigin.set(status.isCargoOrigin())
        pickAlm.set(status.isPickAlm())
        cargoAlm.set(status.isCargoAlm())
    }

}


















